<?php
/**
 * Created by PhpStorm.
 * User: front
 * Date: 2018/8/3
 * Time: 16:39
 */

namespace App\Services;

use App\Exceptions\ApiException;
use App\Models\FailFriend;
use App\Models\JpushMobile;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;
use App\Services\Pusher;


class UserService
{

    /*
     * 新增/编辑用户黑名单
     * $userInfo 当前用户
     * $params 参数
     * */
    public static function option_fail_friend($userInfo, $params)
    {
        $where = [ ['uid', '=', $userInfo->id],['friend_uid', '=', $params['friend_uid']] ];

        $rowsModel = FailFriend::getRows($where);
        if(empty($rowsModel)){
            $rowsModel = new FailFriend();
            $rowsModel->uid = $userInfo->id;
            $rowsModel->friend_uid = $params['friend_uid'];
        }
        $rowsModel->update_t = time();
        $rowsModel->status = $params['status'];
        if($rowsModel->save())
            return true;

        return false;
    }

    /*
     * 登录生成ticket 和 校验是否发送极光下线通知
     *
     * $params array
     * $params['mobile_unique_code']  移动设备唯一标识
     * $params['registration_id']  极光id
     * */
    public static function login_send_jpush($userInfo, $params){

        $ClientSource = Input::get('ClientSource',3);
        $ClientSystem = Input::get('ClientSystem','MicroMessenger');
        if(empty($params) || !isset($params['mobile_unique_code']))
            return false;

        // 用户ticket 是否存在
        $userInfo_ticket_info = DB::table('user_tickets')
        ->where([
            ['client_source','=',$ClientSource],
            ['uid','=',$userInfo->id],
        ])->first();

        // 生成新 ticket
        $ticket = guid();
        if(empty($userInfo_ticket_info)){
            $sso = array(
                'ticket' => $ticket,
                'uid' => $userInfo->id,
                'status' => 1, //{活动中=1,正常退出=2,异常=0}
                'ip_address' => get_ip(),
                'client_source' => $ClientSource,
                'client_system' => $ClientSystem,
                'created_at'    => date('Y-m-d H:i:s',time()),
                'updated_at'    => date('Y-m-d H:i:s',time())
            );
            $createTicket = DB::table('user_tickets')->insertGetId($sso);
        }else{
            // 极光推送消息    // 登录： ticket 活动中 ，  极光id存在， 上次登录用户的极光id 存在，  两个极光id不一致
            if($userInfo_ticket_info->status == 1 && (!empty($params['registration_id']) && !empty($userInfo->registration_id ) && $userInfo->registration_id != $params['registration_id']) ){
                try {
                    Pusher::message($userInfo->registration_id, '您的账户在别处登陆', ['type' => 4]);
                } catch (\Exeption $e) {
                    Log::info(__FILE__, 'login', $e->getMessage(), 'registration_id'. $userInfo->registration_id);
                }
            }
            $data = array(
                'ticket' => $ticket,
                'updated_at'    => date('Y-m-d H:i:s',time()),
                'status' => 1
            );
            $createTicket = DB::table('user_tickets')->where('uid', '=', $userInfo->id)->update($data);
        }

        // 存储极光设备标识
        if(!empty($params['registration_id'])){
            JpushMobile::saveJpush($params);
            DB::table('users')->where('id', '=', $userInfo->id)->update(['registration_id'=>$params['registration_id']]);
        }

        if($createTicket){
            cache(['ticket_'.$ClientSource.$userInfo->id => $ticket], config('basic.ticket_cache_time'));
            return $ticket;
        }

        return false;
    }
}